package view.frames;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

import model.Actions;
import utilities.EPatterns;
import utilities.IOToolKit;
import utilities.PatternsInfoLoader;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class PatternRecommendatorFrame extends javax.swing.JFrame {

	/**
	 * CONSTANTES
	 */
	private static final String FEEDBACK_QUESTION = "Do you want to apply this Design Pattern?";
	private static final String PATTERN_DETECTED = " Design Pattern";
	private static final String CLASS_DIAGRAM_TITLE = "Structure";
	private static final String INFO_TITLE_LABEL = "Information";
	
	/**
	 * 
	 */
	private EPatterns pattern = EPatterns.AbstractFactory;
	private Actions actions;
	private String chain = new String();
	
	/**
	 * GUI
	 */
	private static final long serialVersionUID = 1L;
	private JLabel feedbackLabel;
	private JButton noButton;
	private JButton yesButton;
	private JFrame classesMapperFrame;
	private JLabel patternNameLabel;
	private BufferedImage patternImage;
	private JLabel classDiagramTitleLabel;
	private JEditorPane patternInfoText;
	private JScrollPane scrollPane;
	private JButton cancelButton;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				PatternRecommendatorFrame inst = new PatternRecommendatorFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public PatternRecommendatorFrame() {
		super();
		chain = new String();
		initGUI();
	}
	
	public PatternRecommendatorFrame(EPatterns pattern, String chain, Actions actions) {
		super();
		this.pattern = pattern;
		this.chain = chain;
		this.actions = actions;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			setPreferredSize(new Dimension(1100, 700));
			this.setResizable(false);
			GridBagLayout thisLayout = new GridBagLayout();
			this.setTitle("Pattern Advisor - Design Pattern");
			thisLayout.rowWeights = new double[] {0.01, 0.001, 0.01, 0.001, 0.01};
			thisLayout.rowHeights = new int[] {1, 1, 7, 1, 1};
			thisLayout.columnWeights = new double[] {0.1, 0.1};
			thisLayout.columnWidths = new int[] {7, 7};
			this.setIconImage(new ImageIcon(getClass().getClassLoader().getResource("resources/icons/wizard.png")).getImage());
			getContentPane().setLayout(thisLayout);
			getContentPane().setBackground(new java.awt.Color(255,255,255));
			setLayout(new BorderLayout());
			JLabel background=new JLabel(new ImageIcon(getClass().getClassLoader().getResource("resources/images/background_3.png")));
			add(background);
			background.setLayout(thisLayout);

			{
				
				patternNameLabel = new JLabel();
				patternNameLabel.setText(pattern.getAlternativeName() + PATTERN_DETECTED);
				patternNameLabel.setFont(new Font("Curier New", 1, 23));
				background.add(patternNameLabel, new GridBagConstraints(0, 0, GridBagConstraints.REMAINDER, GridBagConstraints.REMAINDER, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0));
			}
			{
				classDiagramTitleLabel = new JLabel();
				classDiagramTitleLabel.setText(CLASS_DIAGRAM_TITLE);
				classDiagramTitleLabel.setFont(new java.awt.Font("Curier New",Font.BOLD,18));
				background.add(classDiagramTitleLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30, 40, 0, 0), 0, 0));
			}
			{
				patternImage = ImageIO.read(new File(System.getProperty("user.dir") + PatternsInfoLoader.getPatternProperty(pattern, PatternsInfoLoader.PATTERN_IMAGE_PATH))); 
				JLabel picLabel = new JLabel(new ImageIcon(patternImage));
				picLabel.setPreferredSize(new Dimension(650, 450));
				picLabel.setMaximumSize(new Dimension(650, 450));
				picLabel.setMinimumSize(new Dimension(650, 450));
				background.add(picLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				
			}
			{
				patternInfoText = new JEditorPane(new HTMLEditorKit().getContentType(),"");
				patternInfoText.setEditable(false);
				patternInfoText.setContentType("text/html");  
				patternInfoText.setText((String) PatternsInfoLoader.getPatternProperty(pattern, PatternsInfoLoader.PATTERN_DESCRIPTION));
				patternInfoText.setBackground(Color.white);
				patternInfoText.setBorder(BorderFactory.createCompoundBorder(null,null));
				Font font = new Font("Segoe UI", Font.PLAIN, 24);
			    String bodyRule = "body { font-family: " + font.getFamily() + "; }";
			    ((HTMLDocument)patternInfoText.getDocument()).getStyleSheet().addRule(bodyRule);
				patternInfoText.setFont(new java.awt.Font("Curier New",0,12));
				scrollPane = new JScrollPane(patternInfoText);
				scrollPane.setPreferredSize(new java.awt.Dimension(410, 500));
				scrollPane.setFont(new java.awt.Font("Curier New",0,12));
				background.add(scrollPane, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(-52, 0, 0, 0), 0, 0));
				scrollPane.setBorder(new LineBorder(new java.awt.Color(255,255,255), 1, false));
				patternInfoText.setCaretPosition(0);
			}
			{
				feedbackLabel = new JLabel();
				feedbackLabel.setText(FEEDBACK_QUESTION);
				feedbackLabel.setFont(new java.awt.Font("Curier New",1,17));
				background.add(feedbackLabel, new GridBagConstraints(0, 3, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 10, 0, 0), 0, 0));
			}
			{
				yesButton = new JButton();
				yesButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("resources/icons/ok-icon.png")));
				yesButton.setText("Yes");
				yesButton.setToolTipText("Press Yes to continue");
				yesButton.setFont(new Font("Curier New",0,12));
				yesButton.addMouseListener(new MouseAdapter() {
					public void mousePressed(MouseEvent evt) {
						yesButtonMousePressed(evt);
					}
				});
				background.add(yesButton, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 300, 0, 0), 20, 5));
			}
			{
				noButton = new JButton();
				noButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("resources/icons/cross-icon.png")));
				noButton.setText("No");
				noButton.setToolTipText("Press No to choose a pattern from a list");
				noButton.setFont(new java.awt.Font("Curier New",0,12));
				noButton.addMouseListener(new MouseAdapter() {
					public void mousePressed(MouseEvent evt) {
						noButtonMousePressed(evt);
					}
				});
				background.add(noButton, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(10, 0, 0, 300), 25, 5));
			}
			{
				cancelButton = new JButton();
				cancelButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("resources/icons/cancel.png")));
				cancelButton.setText("Cancel");
				cancelButton.setToolTipText("Press Cancel to exit the wizard");
				cancelButton.setFont(new java.awt.Font("Curier New",0,12));
				cancelButton.addMouseListener(new MouseAdapter() {
					public void mousePressed(MouseEvent evt) {
						cancelButtonMousePressed(evt);
					}
				});
				background.add(cancelButton, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(10, 0, 0, 40), 20, 5));
			}
			
			pack();
			setSize(1100, 700);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

	protected void yesButtonMousePressed(MouseEvent evt) {
		
		this.dispose();
		
		// Guardo el feedback
		IOToolKit.saveFeedbackChain((String)PatternsInfoLoader.getPatternProperty(pattern, PatternsInfoLoader.PATTERN_FEEDBACK_FILE_PATH), this.chain);
		
		classesMapperFrame = new ClassesMapperFrame(pattern, actions, this);
	}

	protected void noButtonMousePressed(MouseEvent evt) {

		this.dispose();
		
		JFrame patternsListFrame;
		
		patternsListFrame = new PatternsListFrame(pattern,chain,actions);
	
		patternsListFrame.setLocationRelativeTo(null);
		patternsListFrame.setVisible(true);
	}
	
	protected void cancelButtonMousePressed(MouseEvent evt) {
		this.dispose();
	}
}
